package com.tianqi.modules.tqs.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson2.JSON;
import com.tianqi.common.exception.ApiException;
import com.tianqi.modules.tqs.dto.CommonExcelData;
import com.tianqi.modules.tqs.enums.CoreTypeEnum;
import com.tianqi.modules.tqs.mapper.TqCoreDataMapper;
import com.tianqi.modules.tqs.model.TqCoreData;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

/**
 * Excel数据的读取类
 *
 * @author zhangzedi
 * @date 2023/8/5 23:28
 **/
@Slf4j
public class CommonExcelListener implements ReadListener<CommonExcelData> {

	/**
	 * 定义一个全局的读取header的map，方便其他地方使用改数据，当然要是你存进数据库那就更方便取了
	 */
	private final Map<Integer, ReadCellData<?>> userHeaderMap = new HashMap<>();

	/**
	 * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
	 */
	private static final int BATCH_COUNT = 100;
	/**
	 * 缓存的数据
	 */
	private List<CommonExcelData> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
	private final TqCoreDataMapper tqCoreDataMapper;

	private String sheetName;

	public CommonExcelListener(TqCoreDataMapper tqCoreDataMapper) {
		this.tqCoreDataMapper = tqCoreDataMapper;
	}

	@Override
	public void invoke(CommonExcelData commonExcelData, AnalysisContext analysisContext) {
		log.info("解析到一条数据:{}", JSON.toJSONString(commonExcelData));
		// 获取当前sheet名称
		sheetName = analysisContext.readSheetHolder().getSheetName();
		cachedDataList.add(commonExcelData);
		// 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
		if (cachedDataList.size() >= BATCH_COUNT) {
			saveData();
			// 存储完成清理 list
			cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
		}
	}

	/**
	 * 读取标题行
	 *
	 * @param headMap
	 * @param context
	 */
	@Override
	public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
		log.info("解析到一条头数据");
		// 存到全局的map中
		userHeaderMap.putAll(headMap);
		System.out.println(JSON.toJSONString(headMap.get(0)));
	}

	@Override
	public void doAfterAllAnalysed(AnalysisContext analysisContext) {
		// 这里也要保存数据，确保最后遗留的数据也存储到数据库
		saveData();
		log.info("所有数据解析完成！");
	}

	/**
	 * 加上存储数据库
	 */
	private void saveData() {
		log.info("{}条数据，开始存储数据库！", cachedDataList.size());
		// 根据sheet名称查询类别
		CoreTypeEnum typeEnum = CoreTypeEnum.getByName(sheetName);
		if (Objects.isNull(typeEnum)) {
			throw new ApiException("未查询到类型名称");
		}
		List<TqCoreData> tqCoreDataList = new ArrayList<>();
		for (CommonExcelData rowData : cachedDataList) {
			// 从第二行循环处理行数据
			String colName = rowData.getColName();
			if (StringUtils.isNotBlank(rowData.getCol1())) {
				//获取头数据
				ReadCellData<?> readCellData = userHeaderMap.get(1);
				TqCoreData tqCoreData =
						TqCoreData.builder().conditionOne(colName).conditionTwo(readCellData.getStringValue()).value(rowData.getCol1())
								.type(typeEnum.getType()).typeName(typeEnum.getName()).build();
				tqCoreDataList.add(tqCoreData);
			}
			if (StringUtils.isNotBlank(rowData.getCol2())) {
				//获取头数据
				ReadCellData<?> readCellData = userHeaderMap.get(2);
				TqCoreData tqCoreData =
						TqCoreData.builder().conditionOne(colName).conditionTwo(readCellData.getStringValue()).value(rowData.getCol2())
								.type(typeEnum.getType()).typeName(typeEnum.getName()).build();
				tqCoreDataList.add(tqCoreData);
			}
			if (StringUtils.isNotBlank(rowData.getCol3())) {
				//获取头数据
				ReadCellData<?> readCellData = userHeaderMap.get(3);
				TqCoreData tqCoreData =
						TqCoreData.builder().conditionOne(colName).conditionTwo(readCellData.getStringValue()).value(rowData.getCol3())
								.type(typeEnum.getType()).typeName(typeEnum.getName()).build();
				tqCoreDataList.add(tqCoreData);
			}
			if (StringUtils.isNotBlank(rowData.getCol4())) {
				//获取头数据
				ReadCellData<?> readCellData = userHeaderMap.get(4);
				TqCoreData tqCoreData =
						TqCoreData.builder().conditionOne(colName).conditionTwo(readCellData.getStringValue()).value(rowData.getCol4())
								.type(typeEnum.getType()).typeName(typeEnum.getName()).build();
				tqCoreDataList.add(tqCoreData);
			}
			if (StringUtils.isNotBlank(rowData.getCol5())) {
				//获取头数据
				ReadCellData<?> readCellData = userHeaderMap.get(5);
				TqCoreData tqCoreData =
						TqCoreData.builder().conditionOne(colName).conditionTwo(readCellData.getStringValue()).value(rowData.getCol5())
								.type(typeEnum.getType()).typeName(typeEnum.getName()).build();
				tqCoreDataList.add(tqCoreData);
			}
			if (StringUtils.isNotBlank(rowData.getCol6())) {
				//获取头数据
				ReadCellData<?> readCellData = userHeaderMap.get(6);
				TqCoreData tqCoreData =
						TqCoreData.builder().conditionOne(colName).conditionTwo(readCellData.getStringValue()).value(rowData.getCol6())
								.type(typeEnum.getType()).typeName(typeEnum.getName()).build();
				tqCoreDataList.add(tqCoreData);
			}
			if (StringUtils.isNotBlank(rowData.getCol7())) {
				//获取头数据
				ReadCellData<?> readCellData = userHeaderMap.get(7);
				TqCoreData tqCoreData =
						TqCoreData.builder().conditionOne(colName).conditionTwo(readCellData.getStringValue()).value(rowData.getCol7())
								.type(typeEnum.getType()).typeName(typeEnum.getName()).build();
				tqCoreDataList.add(tqCoreData);
			}
			if (StringUtils.isNotBlank(rowData.getCol8())) {
				//获取头数据
				ReadCellData<?> readCellData = userHeaderMap.get(8);
				TqCoreData tqCoreData =
						TqCoreData.builder().conditionOne(colName).conditionTwo(readCellData.getStringValue()).value(rowData.getCol8())
								.type(typeEnum.getType()).typeName(typeEnum.getName()).build();
				tqCoreDataList.add(tqCoreData);
			}
			if (StringUtils.isNotBlank(rowData.getCol9())) {
				//获取头数据
				ReadCellData<?> readCellData = userHeaderMap.get(9);
				TqCoreData tqCoreData =
						TqCoreData.builder().conditionOne(colName).conditionTwo(readCellData.getStringValue()).value(rowData.getCol9())
								.type(typeEnum.getType()).typeName(typeEnum.getName()).build();
				tqCoreDataList.add(tqCoreData);
			}
			if (StringUtils.isNotBlank(rowData.getCol10())) {
				//获取头数据
				ReadCellData<?> readCellData = userHeaderMap.get(10);
				TqCoreData tqCoreData =
						TqCoreData.builder().conditionOne(colName).conditionTwo(readCellData.getStringValue()).value(rowData.getCol10())
								.type(typeEnum.getType()).typeName(typeEnum.getName()).build();
				tqCoreDataList.add(tqCoreData);
			}
			if (StringUtils.isNotBlank(rowData.getCol11())) {
				//获取头数据
				ReadCellData<?> readCellData = userHeaderMap.get(11);
				TqCoreData tqCoreData =
						TqCoreData.builder().conditionOne(colName).conditionTwo(readCellData.getStringValue()).value(rowData.getCol11())
								.type(typeEnum.getType()).typeName(typeEnum.getName()).build();
				tqCoreDataList.add(tqCoreData);
			}
			if (StringUtils.isNotBlank(rowData.getCol12())) {
				//获取头数据
				ReadCellData<?> readCellData = userHeaderMap.get(12);
				TqCoreData tqCoreData =
						TqCoreData.builder().conditionOne(colName).conditionTwo(readCellData.getStringValue()).value(rowData.getCol12())
								.type(typeEnum.getType()).typeName(typeEnum.getName()).build();
				tqCoreDataList.add(tqCoreData);
			}
		}
		// 读取数据装换为要保存的数据
		tqCoreDataMapper.inertBatch(tqCoreDataList);
		log.info("存储数据库成功！");
	}
}
